import Message from "element-ui/packages/message/src/main";

const defaultsurl = {
        addformurl: '/admin/dev/form/new',
        saveformurl: '/admin/dev/form/submit', // 表单提交地址
        loadformtree: '/admin/dev/form',
        deleteform: '/admin/dev/form/delete',
        getformoption: '/admin/dev/form/data',
        saveformoption: '/admin/dev/form/data/submit',
        editform: '/admin/dev/form/edit',
        forminfo: function(id) {
            return '/admin/dev/form/' + id
        },

        dicload: function(id) {
            return '/admin/sys/dic/data/' + id
        },
        tablelist: '/admin/dev/metadada/table',
        metaData: '/admin/dev/metadada/field',
        formMakingTableList: '/admin/dev/form/table',
        mainTreeUrl: '/admin/sys/category',

        treeformnew: '/admin/sys/category/new',
        treeformsave: '/admin/sys/category/submit',

        //grid
        gridColumncfg: '/admin/sys/curd/config',
        gridDataUrl: '/admin/sys/curd',
        addNewData: '/admin/sys/curd/new',
        normalFormsave: '/admin/sys/curd/submit',
        normaleFormEdit: '/admin/sys/curd/edit',
        normaleGridDelete: '/admin/sys/curd/delete',
        saveUserGridVisableColumn:"/admin/sys/curd/user",//保存用户显示列配置
        clearUserGridVisableColumn:"/admin/sys/curd/clear",//清除用户显示列配置
        gridExportExcel:"/admin/sys/curd/export",//导出excel

        //user
        login: '/admin/user/logon',
        loginout: '/admin/user/logout',
        loadMenu: '/admin/user/right/menu',
        userinfo: '/admin/user/info',
        userpasswprd: '/admin/user/app/info/password',

        //devField：开发平台字段维护功能
        devFieldUrl: '/admin/dev/api/field',
        devFieldNewUrl: '/admin/dev/api/field/new',
        devFieldEditUrl: '/admin/dev/api/field/edit',
        devFieldSubmitUrl: '/admin/dev/api/field/submit',
        devFieldDeleteUrl: '/admin/dev/api/field/delete',

        //tree
        treeConfigUrl: '/admin/sys/category/config',
        defaultTreeDataUrl: '/admin/sys/category',
        normalDicTree: function(dickey) {
            return "/admin/sys/dic/tree/" + dickey
        }, //树形字典弹出控件-请求字典树事件
        normalDicList: function(dickey) {
            return "/admin/sys/dic/common/" + dickey
        }, //树形字典常用列表
        normalDicQuery: "/admin/sys/dic/tree", //通用查询字典列表
        normalDicSave: "/admin/sys/dic/common/submit", //树形字典常用保存
        //file
        fileupload: '/admin/sys/file/upload',
        //图片文件加载
        imageLoad: '/admin/sys/file/photo',
        //文件初始化
        fileInit: '/admin/sys/file/init',
        //文件下载
        fileDownload: '/admin/sys/file/download',
        //文件删除
        fileDelete: '/admin/sys/file/delete',
        // 首页总数女少非
        indexData: '/zzbhr/person/IndexAction?method=indexData',

        //app
        getLastYclDate:"statistics/LeaderUnit?method=getLastYclDate",
        dataPretreat:"statistics/LeaderUnit?method=dataPretreat",
        expDatatoPb:"statistics/LeaderUnit?method=expDatatoPb"
    }
const basepath = 'http://fwq.beiktech.com:8000';
const socketUrl = {
}
const urlModulesFiles = require.context('./urlModules', true, /\.js$/);
const urlWebsocket = {};
const urlModules = {};
urlModulesFiles.keys().reduce((modules, modulePath) => {
    // set './app.js' => 'app'
    const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
    const value = urlModulesFiles(modulePath);
    let urls = value.default.urls;
    for (let key in urls) {
        if (defaultsurl[key] != undefined) {
            console.log(moduleName, `${moduleName}->Urls中${key}重复设置`)
        }
        defaultsurl[key] = urls[key];
    }
    for (let key in value.default.websocketUrl) {
        if (socketUrl[key]) {
            console.log(moduleName, `${moduleName}->websocketUrl中${key}重复设置`)
        }
        socketUrl[key] = value.default.websocketUrl[key];
    }
    urlModules[moduleName] = value.default.urls;
    urlWebsocket[moduleName] = value.default.websocketUrl || {};
}, {})
export const realUrlForSocket = function(subject, urlparam) {
    if (_.isEmpty(subject)) {
        Message("Subject不能为空")
        return;
    }
    let url = subject;
    let tem = socketUrl[subject];
    if (subject.indexOf(".") != -1) {
        let s = url.subject(".");
        tem = urlWebsocket[s[0]][s[1]];
    }
    if (tem != undefined) {
        if (typeof tem == 'function') {
            url = tem(...urlparam)
        } else {
            url = tem
        }
    }
    if (url && url.substr(0, 4) != 'http') url = basepath + url;
    return url;
}
const realUrl = function(url, urlparam) {
    if (_.isEmpty(url)) {
        Message("Url不能为空")
        return;
    }
    if (url && url.substr(0, 4) == 'http') return url;
    let tem = defaultsurl[url];
    if (url.indexOf(".") != -1) {
        let s = url.split(".");
        if(urlModules[s[0]])
        tem = urlModules[s[0]][s[1]];
    }
    if (tem != undefined) {
        if (typeof tem == 'function') {
            url = tem(...urlparam)
        } else {
            url = tem;
        }

    }
    if (url && url.substr(0, 4) != 'http') url = basepath + url;
    url+=(url.indexOf("?")==-1?"?":"&")+`_APP=${appname}`;
    return url;
}
export const appname = "appraise" //appraise
export default {
    appname,
    defaultsurl,
    basepath,
    realUrl,
    realUrlForSocket
}
